package com.wugh.fast.module.mapper.interceptor.plugin;

import com.wugh.fast.module.mapper.EntityInfo;
import lombok.Getter;
import tk.mybatis.mapper.entity.EntityColumn;

import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.util.Set;

/**
 * 说明: 注解插件抽象类
 *
 * @author wugh
 * @date 2019-01-17 11:10
 */
public abstract class AbstractAnnotationPlugin<A extends Annotation> extends AbstractPlugin {
    /**
     * 注解类
     */
    @Getter
    private Class<A> annotationClass;
    @Getter
    private A annotation;
    @Getter
    private EntityColumn column;

    public AbstractAnnotationPlugin() {
        this.annotationClass = (Class<A>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    @Override
    public boolean support(EntityInfo entityInfo) {
        super.support(entityInfo);
        Set<EntityColumn> columns = entityInfo.getEntityTable().getEntityClassColumns();
        for (EntityColumn column : columns) {
            if (column.getEntityField().isAnnotationPresent(annotationClass)) {
                this.annotation = column.getEntityField().getAnnotation(annotationClass);
                this.column = column;
                return true;
            }
        }
        return false;
    }

}
